package com.koron.system.configSettings.controller;

import com.koron.bean.base.Response;
import com.koron.common.core.business.configsettings.bean.ConfigDetail;
import com.koron.common.core.business.configsettings.service.ConfigDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 系统管理-配置详情管理{@code ConfigDetail}控制层
 *
 * <p>主要提供：增加、删除、修改、查询功能</p>
 *
 * @author 朱超
 * @since 2021-09-02
 */
@RestController
@RequestMapping("sys/ConfigDetail")
@Api(tags = "系统管理-配置详情接口")
public class ConfigDetailController {

    @Autowired
    private ConfigDetailService configDetailService;

    /**
     * 新增单条数据
     *
     * @param configDetail
     * @return Response
     * @see Response
     * @version 1.0
     */
    @PostMapping("insert")
    @ApiOperation(value = "插入单条配置详情")
    public Response checkBeforeInsert(@RequestBody @Valid ConfigDetail configDetail) {
        return ADOConnection.runTask(configDetailService, "checkBeforeInsert", Response.class, configDetail);
    }

    /**
     * 逻辑删除单条数据
     *
     * @param id 主键
     * @return Response对象
     * @see Response
     * @version 1.0
     */
    @DeleteMapping("delete")
    @ApiOperation(value = "根据id删除单条配置分类")
    public Response delete(@RequestParam("id") String id) {
        return ADOConnection.runTask(configDetailService, "checkBeforeDelete", Response.class, id);
    }

    /**
     * 修改数据
     *
     * @param configDetail
     * @return Response
     * @see Response
     * @version 1.0
     */
    @PutMapping("update")
    @ApiOperation(value = "修改单条配置详情")
    public Response update(@RequestBody @Valid ConfigDetail configDetail) {
        return ADOConnection.runTask(configDetailService, "checkBeforeUpdate", Response.class, configDetail);
    }

    @GetMapping("getList")
    @ApiOperation(value = "根据传入的配置项id和租户，返回相应的数据")
    public Response getList(@RequestParam("configId") String configId, @RequestParam(value = "appid", required = false) String appid) {
        return ADOConnection.runTask(configDetailService, "getList", Response.class, configId, appid);
    }

    @GetMapping("getByClassifyCodeAndCodeAndAppidAndKey")
    @ApiOperation("根据传入的classifyCode,code,appid,key，返回对应的数据")
    public Response test(@RequestParam("classifyCode") String classifyCode, @RequestParam("code") String code,
                         @RequestParam("appid") String appid, @RequestParam("key") String key, SessionFactory factory) {
        ConfigDetail configDetail= configDetailService.getByClassifyCodeAndCodeAndAppidAndKey(factory,classifyCode,code,appid,key);
        return Response.success(configDetail);
    }
}
